<template>
  <div class="wrap">
    <div v-for="item in keys" :key="item">
      <a-form-item label="物品名" :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }">
        <a-input
          :disabled="disabled"
          v-decorator="[`name[${item}]`, {
            rules: [{required: true, message: '请输入！'}]
          }]"/>
      </a-form-item>
      <a-row>
        <a-col :span="12">
          <a-form-item label="类别" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
            <a-input
              :disabled="disabled"
              v-decorator="[`type[${item}]`, {
                rules: [{required: true, message: '请输入！'}]
              }]"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="单位" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
            <a-input
              :disabled="disabled"
              v-decorator="[`unit[${item}]`, {
                rules: [{required: true, message: '请输入！'}]
              }]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-item label="单价" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
            <a-input
              :disabled="disabled"
              v-decorator="[`price[${item}]`, {
                rules: [{required: true, message: '请输入！'}]
              }]"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="数量" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
            <a-input
              :disabled="disabled"
              v-decorator="[`amount[${item}]`, {
                rules: [{required: true, message: '请输入！'}]
              }]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-button v-if="hasDeleteButton" type="danger" style="width: 100%" @click="remove(item)">
          <a-icon type="delete" />删除
        </a-button>
      </a-row>
      <a-divider/>
    </div>
    <a-button v-if="hasAddButton" type="dashed" style="width: 100%" @click="add">
      <a-icon type="plus" />添加
    </a-button>
  </div>
</template>

<script>
export default {
  name: 'StuffInfo',
  props: {
    length: {
      type: Number,
      default: 1
    },
    hasDeleteButton: {
      type: Boolean,
      default: true
    },
    hasAddButton: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  created () {
    // 初始化
    for (let i = 0; i < this.length; i++) {
      this.keys = this.keys.concat(this.id++)
    }
  },
  data () {
    return {
      id: 0,
      keys: [],
      labelCol: { lg: { span: 5 }, sm: { span: 5 } },
      wrapperCol: { lg: { span: 19 }, sm: { span: 19 } }
    }
  },
  methods: {
    add () {
      this.keys = this.keys.concat(this.id++)
    },
    remove (item) {
      this.keys = this.keys.filter(i => i !== item)
    }
  }
}
</script>

<style lang="less" scoped>
.wrap {
  /deep/ .ant-form-item {
    margin-bottom: 5px;
  }
  /deep/ .ant-divider-horizontal {
    margin-top: 5px;
    margin-bottom: 5px;
  }
}
</style>
